package com.shigui.news.model;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;

public class JsonResponse {

	public static final String ERROR_CODE_SUCCESS_STRING = "200";
	
	private Object data;
	private Map<String, Object> status = new HashMap<String, Object>();
	private Map<String, Object> response = new HashMap<String, Object>();
	/**
	 * �����
	 * ��֪��ʲô�����
	 */
	public JsonResponse(){
		this.addErrorCode(ERROR_CODE_SUCCESS_STRING);
		
	}
	public JsonResponse(String errCode, String message){
		
		this.addErrorCode(errCode).addMessage(message);
	}
	/**
	 * ����
	 * @param data
	 */
	public JsonResponse(Object data){
		
		this.addErrorCode(ERROR_CODE_SUCCESS_STRING);
		this.setData(data);
		
	}
	/*
	 * �ն�
	 */
	public String jsonFormatResponse() throws JsonGenerationException, JsonMappingException, IOException{
		
		ObjectMapper jsonMapper = new MyJson();
		jsonMapper.setSerializationInclusion(Inclusion.NON_NULL);
		
		this.response.put("data", data);
		this.response.put("status", this.getStatus());
		
		return jsonMapper.writeValueAsString(this.response);
	}
	
	public JsonResponse addErrorCode(String errCode){
		
		this.status.put("errCode", errCode);
		return this;
	}

	public JsonResponse addMessage(String message){
		
		this.status.put("message", message);
		return this;
	}
	
	
	
	public Object getData() {
		return data;
	}
	public void setData(Object data) {
		this.data = data;
	}
	public Map<String, Object> getStatus() {
		return status;
	}
	public void setStatus(Map<String, Object> status) {
		this.status = status;
	}
	public Map<String, Object> getResponse() {
		return response;
	}
	public void setResponse(Map<String, Object> response) {
		this.response = response;
	}
	
	
}
